package org.fei.ClassDiagram;

import org.fei.ClassDiagram.GenerateDiagramAction;
import org.fei.ClassDiagram.RunAction;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.AbstractAction;
import javax.swing.Action;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectUtils;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionRegistration;
import org.openide.awt.DynamicMenuContent;
import org.openide.filesystems.FileObject;
import org.openide.util.ContextAwareAction;
import org.openide.util.Lookup;

import java.net.URL;
import javax.swing.SwingUtilities;
import org.fei.ClassDiagram.ProjectScanner;
import org.openide.util.RequestProcessor;
/**
 *
 * @author Tomas
 */

@ActionID(id = "org.myorg.helloyou.DemoAction", category = "SomeFolder")
@ActionRegistration(displayName = "Generate Class diagram")
@ActionReference(path = "Projects/Actions")
public class GenerateDiagramProjectPopupAction extends AbstractAction implements ContextAwareAction {
    public @Override void actionPerformed(ActionEvent e) {assert false;}
    public @Override Action createContextAwareInstance(Lookup context) {
        return new ContextAction(context);
    }
    
    // vypinanie tlacitka
    private static boolean enabling = true;
    
    private static final class ContextAction extends AbstractAction {
        private final Project p;
        private ProjectScanner projectScanner;
        // background thread pool
        private static final RequestProcessor RP = new RequestProcessor(GenerateDiagramAction.class);
  
        public ContextAction(Lookup context) {
            p = context.lookup(Project.class);
            String name = ProjectUtils.getInformation(p).getDisplayName();
           
            projectScanner = new ProjectScanner(p);
            if (projectScanner.isProjectValid() && GenerateDiagramProjectPopupAction.enabling) {
                setEnabled(true);                
            }
            else {           
                setEnabled(false);
            }
            putValue(DynamicMenuContent.HIDE_WHEN_DISABLED, true);
            // TODO menu item label with optional mnemonics
            putValue(NAME, "Generate Class diagram");
        }
        
        // zalozit nove vlakno
        // vypnut tlacitko
        // na konci pracovneho vlakna zapnut akciu
        public @Override void actionPerformed(ActionEvent e) {
            GenerateDiagramProjectPopupAction.setEnabling(false);
            RP.post(new RunAction(this, p, projectScanner), 0, Thread.NORM_PRIORITY);
            //SwingUtilities.invokeLater(new RunAction(this, p, projectScanner));
            /*
            AntCompilator.run(projectScanner.getBuildImplXml());
            
            // ziskanie adresara v ktorom su class subory
            ProjectPropertiesScanner propertiesScanner = new ProjectPropertiesScanner(projectScanner.getProjectProperties().getPath());
            FileObject classesDir = p.getProjectDirectory().getFileObject(propertiesScanner.getClassesDir());

            ArrayList<String> classFiles = new ArrayList<String>();
            projectScanner.findFiles(classesDir, "class", classFiles);
            
            ArrayList<String> sources = new  ArrayList<String>();
            projectScanner.findFiles(p.getProjectDirectory(), "java", sources);
            
            ArrayList<URL> urls = new ArrayList<URL>();
            projectScanner.findFilesURL(classesDir, "class", urls);
            ClassFileScanner classScanner = new ClassFileScanner(classFiles, urls, sources);
            */
        }
    }        
    
    public static void setEnabling(boolean enabling) {
            GenerateDiagramProjectPopupAction.enabling = enabling;
    }
}